<template>
    <div>
        <el-card class="box-card">
            <div>
                <el-input
                        size="small"
                        style="width: 300px;margin-right: 10px;"
                        placeholder="添加职位..."
                        prefix-icon="el-icon-plus"
                        @keydown.enter.native="addPosition(pos.name)"
                        v-model="pos.name">
                </el-input>
                <el-button icon="el-icon-plus" size="small" type="primary" style="vertical-align: top;"
                           @click="addPosition(pos.name)">添加
                </el-button>
            </div>
            <div class="mian-table" style="width: 600px">
                <el-table
                        :data="positions"
                        border
                        @selection-change="select"
                        size="small">
                    <el-table-column
                            type="selection"

                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="职位名称"
                            width="120">
                    </el-table-column>
                    <el-table-column
                            prop="createDate"
                            label="创建时间"
                            width="120">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    @click="handleEdit(scope.$index, scope.row)">编辑
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="danger"
                                    @click="handleDelete(scope.$index, scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-button
                        style="margin-top: 12px"
                        type="danger"
                        @click="deleteSelections"
                        :disabled="this.multiSelections.length == 0">
                    批量删除
                </el-button>

            </div>
        </el-card>

        <el-dialog
                title="修改职位"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <el-tag>职位名称</el-tag>
                <el-input class="updatePosInput" v-model="updataPos.name"></el-input>
                </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="doUpdata">确 定</el-button>
             </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PosManager",
        data() {
            return {
                pos: {
                    name: ''
                },
                positions: [],
                updataPos:{
                    name:''
                },
                dialogVisible: false,
                multiSelections:[],
            }
        },
        methods: {
            deleteSelections(){
                this.$confirm('此操作将永久删除【' + this.multiSelections.length + '】条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multiSelections.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    })
                    this.deleteRequest("/system/basic/pos/" + ids).then(resp => {
                        if (resp) {
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            select(val){
                this.multiSelections = val;
            },
            doUpdata(){
                this.putRequest("/system/basic/pos/",this.updataPos).then(resp=>{
                    this.initPositions();
                    this.dialogVisible = false;
                })
            },
            addPosition(data) {
                if (data) {
                    this.postRequest("/system/basic/pos/", this.pos).then(resp => {
                        if (resp) {
                            this.initPositions();
                            this.pos.name = '';
                        }
                    })
                } else {
                    this.$message.warning("职位名称不能为空！")
                }
            },
            handleEdit(index, data) {
                Object.assign(this.updataPos,data);
                this.dialogVisible =true;
            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除该' + data.name + ', 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/" + data.id).then(resp => {
                        if (resp) {
                            this.initPositions();
                            this.pos.name = '';

                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initPositions() {
                this.getRequest("/system/basic/pos/").then(resp => {
                    if (resp) {
                        this.positions = resp.data;
                    }
                })
            }
        },
        mounted() {
            this.initPositions();
        }
    }
</script>

<style>
    .updatePosInput{
        width: 300px;
        margin-left: 10px;
    }
    .mian-table {
        margin-top: 30px;
    }

    .box-card {
        width: 640px;
        box-shadow: 0 0 25px #cac6c6;
    }
</style>